﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using K.EnterpriseLibary;
using IBatisNet.DataAccess;
using K.Persistence.Interface;
using K.Persistence.MapperDao;
namespace K.Service
{
    public class CartonService
    {
        private static object _synRoot = new object();
        private static CartonService _instance;
  
        
        private IDaoManager _daoManager = null; //定义一个空的DaoManger
        private ICartonDao _cartonDao = null;  //定义一个空的Dao
        private ISequenceDao _sequenceDao = null; //定义一个空的SequenceDao，用于产生唯一ID

        /// <summary>
        /// //静态方法返回静态实例 每个Server采用单例模式
        /// </summary>
        /// <returns></returns>
        public static CartonService GetInstance()
        {
            if (_instance == null)
            {
                lock (_synRoot) //锁定对象，设置一个临界区。
                {
                    if (_instance == null)
                    {

                        //取得实例
                        _instance = new CartonService();

                    }
                }
            }
            return _instance;  
        }
        private CartonService()
        {
            _daoManager = ServiceConfig.GetInstance().daoManager; //通过Dao
            _cartonDao = _daoManager.GetDao(typeof(ICartonDao)) as ICartonDao;
            _sequenceDao = _daoManager.GetDao(typeof(ISequenceDao)) as ISequenceDao;


        }
        public void InsertCarton(Carton carton)
        {
            _daoManager.BeginTransaction();
            try
            {
                
                carton.Id = GetNextId();
                _cartonDao.InsertCarton(carton);
                _daoManager.CommitTransaction();
            }
            catch (Exception e)
            {
                _daoManager.RollBackTransaction();
                throw;
            }
        }

        public void UpdateCarton(Carton carton)
        {
            _daoManager.BeginTransaction();
            try
            {
                _cartonDao.UpdateCarton(carton);
                _daoManager.CommitTransaction();
            }
            catch (Exception e)
            {
                _daoManager.RollBackTransaction();
                throw;
            }
        }

        public Carton  SelectCarton(Carton carton)
        {
            _daoManager.BeginTransaction();
            Carton SelectCarton = new Carton();
            try
            {
                SelectCarton = _cartonDao.SelectCarton(carton);
                _daoManager.CommitTransaction();
            }
            catch (Exception e)
            {
                _daoManager.RollBackTransaction();
                throw;
            }
            return SelectCarton;
        }

        private int GetNextId()
        {
            int id = -1;
            try
            {
                id = _sequenceDao.GetNextId();
 
            }
            catch (Exception e)
            {

                throw;
            }
            return id;
        }
    }
}
